package com.superyee.commonlib.widgets;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.app.ActivityCompat;

import com.superyee.commonlib.R;
import com.superyee.commonlib.utils.AndroidUtil;


/**
 * 自定义ActionBar
 */
public class ActionBar extends RelativeLayout {

    private LayoutInflater mInflater;
    private RelativeLayout mBarView;
    private ImageButton mBackBtn;
    private TextView mTitleView;
    private LinearLayout mActionsView;
    private Button mCommonButton;
    private View mNewIcon;
    private static int mMaxHeight;


    public ActionBar(Context context) {
        super(context);
        initView();
        setBackground(R.color.actionbar_bg_color);
    }

    public ActionBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
        TypedArray typedArray = context.obtainStyledAttributes(attrs,
                R.styleable.MyActionBar);
        CharSequence title = typedArray.getString(R.styleable.MyActionBar_title);
        int background = typedArray.getResourceId(R.styleable.MyActionBar_backgroundcolor, 0);
        if (background != 0) {
            setBackground(background);
        } else {
            setBackground(R.color.actionbar_bg_color);
        }
        if (title != null) {
            setTitle(title);
        }
        typedArray.recycle();
    }

    private void initView() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setTransitionName("comm_actionbar");
        }
        setFitsSystemWindows(true);
        mInflater = LayoutInflater.from(getContext());
        mInflater.inflate(R.layout.comm_actionbar, this);
        mBarView = (RelativeLayout) findViewById(R.id.actionBar);
        mBackBtn = (ImageButton) mBarView.findViewById(R.id.actionbar_back_btn);
        mTitleView = (TextView) mBarView.findViewById(R.id.actionbar_title);
        mActionsView = (LinearLayout) mBarView
                .findViewById(R.id.actionbar_actions);

        mCommonButton = (Button) mBarView.findViewById(R.id.actionbar_right_button);
        mNewIcon = findViewById(R.id.iconNew);
        if (mMaxHeight == 0) {
            mMaxHeight = getResources().getDimensionPixelOffset(R.dimen.actionbar_height) + AndroidUtil.getStatusHeight(getContext());
        }
    }

    public void setBackground(int resid) {
        super.setBackgroundResource(resid);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        if (heightMode == MeasureSpec.EXACTLY) {
            heightSize = heightSize <= mMaxHeight ? heightSize
                    : mMaxHeight;
        }

        if (heightMode == MeasureSpec.UNSPECIFIED) {
            heightSize = heightSize <= mMaxHeight ? heightSize
                    : mMaxHeight;
        }
        if (heightMode == MeasureSpec.AT_MOST) {
            heightSize = heightSize <= mMaxHeight ? heightSize
                    : mMaxHeight;
        }
        int maxHeightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize,
                heightMode);
        super.onMeasure(widthMeasureSpec, maxHeightMeasureSpec);
    }

    public void setBackBG(int id) {
        mBackBtn.setVisibility(View.VISIBLE);
        mBackBtn.setImageResource(id);
    }


    public void setBackAction(OnClickListener listener) {
        mBackBtn.setVisibility(View.VISIBLE);
        mBackBtn.setOnClickListener(listener);
    }

    public void showBack() {
        mBackBtn.setVisibility(View.VISIBLE);
    }

    public void showBack(final Activity activity) {
        setBackAction(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityCompat.finishAfterTransition(activity);
            }
        });
    }

    public void clearBackAction() {
        mBackBtn.setVisibility(View.GONE);
    }

    public void clearRightAction() {
        mCommonButton.setVisibility(View.GONE);
    }


    public Button getRightBtn() {
        mCommonButton.setVisibility(View.VISIBLE);
        return mCommonButton;
    }


    public ImageButton getBackBtn() {
        return mBackBtn;
    }

    /**
     * 控制小红点显示隐藏
     *
     * @param visibility
     */
    public void setNewIconVisibility(boolean visibility) {
        int visibile = visibility ? View.VISIBLE : View.GONE;
        mNewIcon.setVisibility(visibile);
    }

    public void setRightBtn(String label, OnClickListener listener) {
        mCommonButton.setVisibility(View.VISIBLE);
        mCommonButton.setText(label);
        mCommonButton.setOnClickListener(listener);
    }


    public void setRightBackground(int resid) {
        mCommonButton.setBackgroundResource(resid);
    }

//	public void setNavAction(Action action) {
//		mNavBtn.setOnClickListener(this);
//		mNavBtn.setTag(action);
//		mNavBtn.setImageResource(action.getDrawable());
//		mNavLayout.setVisibility(View.VISIBLE);
//	}
//
//	public void clearNavAction() {
//		mNavLayout.setVisibility(View.GONE);
//	}

    public TextView getTitleView() {
        return mTitleView;
    }

    /**
     * @param title
     * @param gravity 对齐方式
     */
    public void setTitle(CharSequence title, int gravity) {
        if (TextUtils.isEmpty(title)) {
            mTitleView.setVisibility(View.GONE);
        } else {
            mTitleView.setVisibility(View.VISIBLE);
        }
        mTitleView.setGravity(gravity);
        mTitleView.setText(title);
    }

    public void setTitle(CharSequence title) {
        setTitle(title, Gravity.CENTER);
    }

    public void setTitle(int resid) {
        setTitle(resid, Gravity.CENTER);
    }

    public void setTitle(int resid, int gravity) {
        if (resid > 0) {
            mTitleView.setVisibility(View.VISIBLE);
            mTitleView.setText(resid);
            mTitleView.setGravity(gravity);
        } else {
            mTitleView.setVisibility(View.GONE);
        }
    }


    /**
     * 在右侧添加View
     *
     * @param resId layout布局
     */
    public View addView(int resId) {
        View view = mInflater.inflate(resId, mActionsView, false);
        mActionsView.addView(view);
        return view;
    }

    /**
     * 添加自定义View
     *
     * @param view
     * @return
     */
    public View addCustomView(View view) {
        mActionsView.addView(view);
        return view;
    }

    public ImageButton addAction(Action action) {
        final int index = mActionsView.getChildCount();
        return addAction(action, index);
    }

    public ImageButton addAction(Action action, int index) {
        ImageButton view = inflateAction(action);
        mActionsView.addView(view, index);
        return view;
    }

    public void removeAllActions() {
        mActionsView.removeAllViews();
    }

    public void removeActionAt(int index) {
        mActionsView.removeViewAt(index);
    }

    public void removeAction(Action action) {
        int childCount = mActionsView.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View view = mActionsView.getChildAt(i);
            if (view != null) {
                final Object tag = view.getTag();
                if (tag instanceof Action && tag.equals(action)) {
                    mActionsView.removeView(view);
                }
            }
        }
    }

    public int getActionCount() {
        return mActionsView.getChildCount();
    }

    public ImageButton getActionAt(int index) {
        return (ImageButton) mActionsView.getChildAt(index);
    }

    private ImageButton inflateAction(final Action action) {
        final ImageButton labelView = (ImageButton) mInflater.inflate(R.layout.comm_actionbar_item, mActionsView, false);

        labelView.setImageResource(action.getDrawable());
        labelView.setEnabled(action.getEnabled());
        labelView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (AndroidUtil.isFastDoubleClick()) {
                    return;
                }
                if (action != null) {
                    action.performAction(labelView);
                }
            }
        });
        return labelView;
    }

    public interface Action {
        public boolean getEnabled();

        public int getDrawable();

        public void performAction(View view);
    }

    public static abstract class AbstractAction implements Action {
        final private int mDrawable;

        public AbstractAction(int drawable) {
            mDrawable = drawable;
        }

        @Override
        public int getDrawable() {
            return mDrawable;
        }
    }

    public static class ClickAction extends AbstractAction {
        private ActionBarClickBack mActionBarClickBack;
        private boolean mIsEnabled = true;

        public ClickAction(int drawable, ActionBarClickBack actionBarClickBack) {
            super(drawable);
            mActionBarClickBack = actionBarClickBack;
        }

        public ClickAction(int drawable, ActionBarClickBack actionBarClickBack,
                           boolean isEnabled) {
            super(drawable);
            mActionBarClickBack = actionBarClickBack;
            mIsEnabled = isEnabled;
        }

        @Override
        public void performAction(View view) {
            if (null != mActionBarClickBack) {
                mActionBarClickBack.onClick(view);
            }
        }

        @Override
        public boolean getEnabled() {
            return mIsEnabled;
        }
    }

    public static interface ActionBarClickBack {
        public void onClick(View view);
    }


    public void setCheckAllItem(OnClickListener listener) {
        mCommonButton.setVisibility(View.VISIBLE);
        mActionsView.setVisibility(View.GONE);
        mCommonButton.setOnClickListener(listener);
    }

    public void clearCheckAllItem() {
        mCommonButton.setVisibility(View.GONE);
        mActionsView.setVisibility(View.VISIBLE);
    }

    public void setBackgroundAlpha(int alpha) {
        Drawable d = getBackground();
        d.setAlpha(alpha);
    }

    public View getCustomView(int resId) {
        View view = mInflater.inflate(resId, null);
        return view;
    }
}
